//
//  TKIMSearchView.m
//  TKIM
//
//  Created by liupm on 15/10/31.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMSearchView.h"
//#import "TKIMColorHelper.h"


@interface TKIMSearchView()
@property(nonatomic,strong) UILabel *line;
@end

@implementation TKIMSearchView

/**
 *  @Author lpm, 16-04-19 14:04:25
 *
 *  @brief
 *
 *  @param frame
 *  @param searchBar
 *  @param ctrl
 *
 *  @return
 */
-(instancetype)initWithFrame:(CGRect)frame searchBar:(UISearchBar *)searchBar containCtrl:(UIViewController *)ctrl
{
    self = [self initWithFrame:frame searchBar:searchBar];
    if (self)
    {
        _containCtrl = ctrl;
    }
    return self;
}


/**
 *  @Author lpm, 16-04-19 14:04:01
 *
 *  @brief
 *
 *  @param frame
 *  @param searchBar
 *
 *  @return
 */
-(instancetype)initWithFrame:(CGRect)frame searchBar:(UISearchBar *)searchBar
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _searchBar = searchBar;
        searchBar.delegate = self;
        _realTimeSearch = YES;
        [self drawView];
    }
    return self;
}


/**
 *  @Author lpm, 15-10-28 17:10:29
 *
 *  @brief  绘制界面
 */
-(void)drawView
{
    UIView *_layerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
    _layerView.backgroundColor = [UIColor blackColor];
    _layerView.alpha = 0.5;
    [self addSubview:_layerView];    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
    [_layerView addGestureRecognizer:tap];
//    [self.searchBar addSubview:self.line];
}


-(UILabel *)line
{
    if (!_line)
    {
        _line = [[UILabel alloc] initWithFrame:CGRectMake(0, self.searchBar.frame.size.height-0.5, self.searchBar.frame.size.width, 0.5f)];
        [_line setBackgroundColor:[TKIMColorHelper stringToColor:@"#B7B7B7"]];
        _line.hidden = NO;
    }
    return _line;
}


-(void)setIsShowBottomBorder:(BOOL)isShowBottomBorder
{
    self.line.hidden = !isShowBottomBorder;
}



-(void)hideKeyboard:(UITapGestureRecognizer *)tap
{
    if([self.searchBar isFirstResponder])
    {
        [self.searchBar resignFirstResponder];
    }
    
    if (self.superview)
    {
        [self removeFromSuperview];
    }
    [self.searchBar setShowsCancelButton:NO animated:YES];
    if (_containCtrl)
    {
        [_containCtrl.navigationController setNavigationBarHidden:NO animated:YES];
    }
}


-(UILabel *)tipLabel
{
    if (!_tipLabel)
    {
//        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-150)];
//        _tipLabel.backgroundColor = [UIColor clearColor];
//        _tipLabel.textAlignment = NSTextAlignmentCenter;
//        _tipLabel.textColor = [UIColor grayColor];
        
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_imageView.frame), self.frame.size.width, 40)];
        _tipLabel.backgroundColor = [UIColor clearColor];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.textColor = [UIColor grayColor];
        
    }
    return _tipLabel;
}

-(UIImageView *)imageView{
  
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width-80)/2, (self.height-65)/2, 80, 65)];
        _imageView.image = TKIMGetImage(@"noSearch");
        _imageView.hidden = YES;
    }
    return _imageView;
}


-(UITableView *)tableView
{
    if (!_tableView)
    {
        //列表
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        _tableView.dataSource  = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 70;
        _tableView.separatorColor = [TKIMColorHelper stringToColor:@"#DCDCDC"];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 70, 0, 0);
        [_tableView setTableFooterView:[UIView new]];
        [_tableView addSubview:self.imageView];
        [_tableView addSubview:self.tipLabel];
    }
    return _tableView;
}

#pragma mark searchbar代理

/**
 *  @Author lpm, 15-11-02 09:11:41
 *
 *  @brief  开始输入搜索文字
 *
 *  @param searchBar
 *
 *  @return
 */
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
    if (_containCtrl)
    {
        [_containCtrl.navigationController setNavigationBarHidden:YES animated:YES];
    }
    if (!self.superview)
    {
        if (_containCtrl)
        {
            [_containCtrl.view addSubview:self];
        }
        else
        {
            [[searchBar superview] addSubview:self];
        }
    }
    return YES;
}



/**
 *  @Author lpm, 15-11-02 09:11:21
 *
 *  @brief  取消按钮点击
 *
 *  @param searchBar
 */
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self resetView];
}


/**
 *  @Author lpm, 15-11-02 09:11:05
 *
 *  @brief  搜索按钮被点击
 *
 *  @param searchBar
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if (_realTimeSearch)
    {
//        [self resetView];
        if([self.searchBar isFirstResponder])
        {
            [self.searchBar resignFirstResponder];
        }
    }
    else
    {
        if (!self.tableView.superview)
        {
            [self addSubview:self.tableView];
        }
        
        if ([searchBar.text isEqualToString:@""])
        {
            [_resultSource removeAllObjects];
            if (self.tableView.superview)
            {
                [self.tableView removeFromSuperview];
            }
        }
        else
        {
            [self searchResult:searchBar.text];
        }
    }
}


/**
 *  @Author lpm, 15-11-02 09:11:54
 *
 *  @brief  搜索文字改变
 *
 *  @param searchBar
 *  @param searchText
 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (!_realTimeSearch)
    {
        return;
    }
    
    if (!self.tableView.superview)
    {
        [self addSubview:self.tableView];
    }
    
    if ([searchText isEqualToString:@""])
    {
        [_resultSource removeAllObjects];
        if (self.tableView.superview)
        {
            [self.tableView removeFromSuperview];
        }
    }
    else
    {
        NSLog(@"%@",self);
        
        if (!_isAsynSearch) {
            
            _resultSource = [self textDidChange:searchText];
            if (_resultSource)
            {
                [_tableView reloadData];
            }
            
        }else{
            //异步搜索的时候又第三方实现刷新
            [self textDidChange:searchText tableview:_tableView];
        }
        
        
    }
    

    
    if (_resultSource.count == 0)
    {
        self.tipLabel.text = @"无搜索结果";
        self.tipLabel.hidden = NO;
        self.imageView.hidden = NO;
    }
    else
    {
        self.tipLabel.text = @"";
        self.imageView.hidden = YES;
        self.tipLabel.hidden = YES;
    }
}



#pragma mark - Table view data source
/**
 *  @Author lpm, 15-10-21 10:10:39
 *
 *  @brief
 *
 *  @param tableView
 *
 *  @return
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

/**
 *  @Author lpm, 15-10-21 10:10:46
 *
 *  @brief
 *
 *  @param tableView
 *  @param section
 *
 *  @return
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultSource.count;
}


/**
 *  @Author lpm, 15-10-21 10:10:57
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self resetView];
}


/**
 *  @Author lpm, 15-11-02 09:11:34
 *
 *  @brief  重置界面
 */
-(void)resetView
{

    self.searchBar.text = @"";
    
    if([self.searchBar isFirstResponder])
    {
        [self.searchBar resignFirstResponder];
    }
    [self.searchBar setShowsCancelButton:NO animated:YES];
    
    if (self.superview)
    {
        [self removeFromSuperview];
    }
    
    if (self.tableView.superview)
    {
        [self.tableView removeFromSuperview];
    }
    
    if (_containCtrl)
    {
        [_containCtrl.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

-(void)resignFirstResponder
{
    [self resetView];
}


/**
 *  @Author lpm, 15-11-02 09:11:25
 *
 *  @brief  提供给子类覆盖
 *
 *  @param text
 *
 *  @return
 */
-(NSMutableArray *)textDidChange:(NSString *)text tableview:(UITableView*)tableview
{
    return nil;
}


-(NSMutableArray *)textDidChange:(NSString *)text
{
    return nil;
}


/**
 *  @Author lpm, 15-11-02 09:11:25
 *
 *  @brief  提供给子类覆盖
 *
 *  @param text
 *
 *  @return
 */
-(void)searchResult:(NSString *)text
{
    
}
@end
